package com.ruoyi.mentor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.ChooseAddBo;
import com.ruoyi.mentor.bo.ChooseEditBo;
import com.ruoyi.mentor.bo.ChooseQueryBo;
import com.ruoyi.mentor.bo.StudentInInstructorQueryBo;
import com.ruoyi.mentor.domain.Choose;
import com.ruoyi.mentor.domain.Instructor;
import com.ruoyi.mentor.domain.SelectedInsVo.SelectedInsVo;
import com.ruoyi.mentor.service.IChooseService;
import com.ruoyi.mentor.service.IStudentService;
import com.ruoyi.mentor.service.impl.InstructorServiceImpl;
import com.ruoyi.mentor.vo.ChooseStuInsVo;
import com.ruoyi.mentor.vo.ChooseVo;
import com.ruoyi.mentor.vo.ExportStudentVo.ExportStudentVo;
import com.ruoyi.mentor.vo.InsStudentVo;
import com.ruoyi.mentor.vo.InstructorVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 志愿选择：学生先选，导师确认Controller
 *
 * @author LH
 * @date 2021-05-21
 */
@Api(value = "志愿选择：学生先选，导师确认控制器", tags = {"志愿选择：学生先选，导师确认管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mentor/choose")
public class ChooseController extends BaseController {

    private final IChooseService iChooseService;

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    IStudentService iStudentService;


    /**
     * 查询志愿选择：学生先选，导师确认列表
     */
    @ApiOperation("查询志愿选择：学生先选，导师确认列表")
    @PreAuthorize("@ss.hasPermi('mentor:choose:list')")
    @GetMapping("/list")
    public TableDataInfo<ChooseStuInsVo> list(@Validated ChooseQueryBo bo) {
        return iChooseService.queryPageList(bo);
    }

    /**
     * 导出志愿选择：学生先选，导师确认列表
     */
    @ApiOperation("导出所有的研究生信息列表")
    @Log(title = "导出所有的研究生信息列表", businessType = BusinessType.EXPORT)
    @GetMapping("/exportAllStudent")
    public AjaxResult exportAllStudent(@Validated ChooseQueryBo bo) {
        List<ExportStudentVo> list = iStudentService.exportAllStudent(bo);
        ExcelUtil<ExportStudentVo> util = new ExcelUtil<ExportStudentVo>(ExportStudentVo.class);
        return util.exportExcel(list, "研究生信息");
    }

    /**
     * 获取志愿选择：学生先选，导师确认详细信息
     */
    @ApiOperation("获取志愿选择：学生先选，导师确认详细信息")
    @PreAuthorize("@ss.hasPermi('mentor:choose:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iChooseService.queryById(id));
    }

    /**
     * 新增志愿选择：学生先选，导师确认
     */
    @ApiOperation("新增志愿选择：学生先选，导师确认")
    @PreAuthorize("@ss.hasPermi('mentor:choose:add')")
    @Log(title = "志愿选择：学生先选，导师确认", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody ChooseAddBo bo) {
        return toAjax(iChooseService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改志愿选择：学生先选，导师确认
     */
    @ApiOperation("修改志愿选择：学生先选，导师确认")
    @PreAuthorize("@ss.hasPermi('mentor:choose:edit')")
    @Log(title = "志愿选择：学生先选，导师确认", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody ChooseEditBo bo) {
        return toAjax(iChooseService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除志愿选择：学生先选，导师确认
     */
    @ApiOperation("删除志愿选择：学生先选，导师确认")
    @PreAuthorize("@ss.hasPermi('mentor:choose:remove')")
    @Log(title = "志愿选择：学生先选，导师确认" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iChooseService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 根据学生id获取已经选择的老师信息
     */
    @ApiOperation("根据学生id获取已经选择的老师信息")
    @GetMapping("/insInfo/{stuId}")
    public AjaxResult getInsInfoByStuId(@PathVariable("stuId") Long stuId) {
        List<SelectedInsVo> instructors = iChooseService.getInsInfoByStuId(stuId);
        return AjaxResult.success(instructors);
    }


    @ApiOperation("根据导师id获取导师选择的学生")
    @GetMapping("/getStudentList")
    public TableDataInfo<InsStudentVo> getMyStudent(@Validated StudentInInstructorQueryBo bo){
        TableDataInfo<InsStudentVo> res = iChooseService.getMyStudent(bo);
        return res;
    }


    @ApiOperation("导师改变学生审核状态")
    @PostMapping("/insExamine")
    @Log(title = "导师改变学生审核状态",businessType = BusinessType.OTHER)
    public AjaxResult insExamine(@RequestParam("stuId") Long stuId,@RequestParam("insId") Long insId,@RequestParam("subId")Long subId,@RequestParam("status") Integer status){
        iChooseService.insExamine(stuId,insId,subId,status);
        return AjaxResult.success();
    }


    @ApiOperation("根据学生id获取选择信息")
    @GetMapping("/getChooseStatus")
    public AjaxResult getChooseStatus(@RequestParam("stuId")Long stuId){
        Choose choose = iChooseService.getOne(new QueryWrapper<Choose>().eq("stu_id", stuId));
        return AjaxResult.success(choose);
    }



}
